import 'package:flutter/material.dart';

class ChildComp extends StatefulWidget {
  const ChildComp({super.key, required this.count, required this.updateCount});

  // 定义属性接收父组件传值
  final int count;
  // 定义属性接收父组件传函数
  final Function(int) updateCount;

  @override
  State<ChildComp> createState() {
    print('子组件 createState');
    return _ChildCompState();
  }
}

class _ChildCompState extends State<ChildComp> {
  @override
  void initState() {
    super.initState();
    print('子组件 initState');
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    print('子组件 didChangeDependencies');
  }

  @override
  Widget build(BuildContext context) {
    print('子组件 build');
    return Container(
      width: 200,
      height: 60,
      color: Colors.red,
      child: Column(
        children: [
          Text('子组件, 父组件传值: ${widget.count}'),
          ElevatedButton(
              onPressed: () {
                // widget.count + 2  12
                // widget.count += 2  widget.count = widget.count + 2
                widget.updateCount(widget.count + 2);
              },
              child: const Text('修改父组件值'))
        ],
      ),
    );
  }

  @override
  void didUpdateWidget(covariant ChildComp oldWidget) {
    super.didUpdateWidget(oldWidget);
    print('子组件 didUpdateWidget');
  }

  @override
  void deactivate() {
    super.deactivate();
    print('子组件 deactivate');
  }

  @override
  void dispose() {
    super.dispose();
    print('子组件 dispose');
  }
}
